package com.chat.business.utils;

import com.chat.common.common.constant.RedisKeyConstant;
import com.chat.common.vo.NoticeVO;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author wei kunJie
 * @date 2024/4/13 17:28:23
 */
@Component
public class RedisHashUtil {
    @Resource
    private HashOperations<String, String, Object> redisHashTemplate;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 发布通知
     *
     * @param noticeVO
     */
    public void publishNotice(NoticeVO noticeVO) {
        //通知的消费者id为key
        redisHashTemplate.put(RedisKeyConstant.NOTICE + noticeVO.getConsumerId(),
                noticeVO.getPublishId()+noticeVO.getNoticeType(),
                noticeVO);
    }

    /**
     * 消费通知
     *
     * @param consumerId
     * @return
     */
    public Map<String, Object> consumeNotice(Long consumerId) {
        Map<String, Object> entries = redisHashTemplate.entries(RedisKeyConstant.NOTICE + consumerId);
        //redisTemplate.delete(RedisKeyConstant.NOTICE + consumerId);
        return entries;
    }
}
